﻿using System;
using System.Configuration;
using System.Linq;
using Controllers;

namespace Web
{
    public partial class EvectionAdd : System.Web.UI.Page
    {
        protected EmpBasicInfo ebi;
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                ebi = Session["ebi"] as EmpBasicInfo;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
            ReqEvectionMain reqEvectionMain = new ReqEvectionMain();
            string maxFormNo = ctx.ReqEvectionMains.Max(r => r.FormNo);
            string formNo = "Form_Evec_000001";
            if (!string.IsNullOrEmpty(maxFormNo))
            {
                formNo = "Form_Evec_" + (int.Parse(maxFormNo.Substring(10)) + 1000001).ToString().Substring(1);
            }
            reqEvectionMain.FormNo = formNo;
            ebi = Session["ebi"] as EmpBasicInfo;
            reqEvectionMain.ReqDate = DateTime.Now;
            reqEvectionMain.EmpId = ebi.Id;
            reqEvectionMain.PreCost = decimal.Parse(PreCost.Value);
            reqEvectionMain.TriperIds =TriperIds.Value;
            reqEvectionMain.Cause = Cause.Value;
            ctx.ReqEvectionMains.InsertOnSubmit(reqEvectionMain);
            ctx.SubmitChanges();
            Response.Write("<script>window.returnValue='" + reqEvectionMain.FormNo + "';window.close();</script>");
        }
    }
}